package com.boot.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.boot.framework.aop.Log;
import com.boot.framework.common.R;
import com.boot.system.dto.SysParamsDTO;
import com.boot.system.service.ISysParamsService;
import com.boot.system.wrapper.SysParamsWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author yangfan
 * @date 2024/2/6
 * @Description
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/params")
@Tag(name = "参数管理")
public class SysParamsController {
    private final ISysParamsService sysParamsService;

    @GetMapping("page")
    @SaCheckPermission(value = "system.params.page")
    @Operation(summary = "分页")
    public R page(@RequestParam Map<String, Object> params) {

        return R.ok(sysParamsService.selectPage(params));
    }

    @GetMapping("list")
    @SaCheckPermission(value = "system.params.list")
    @Operation(summary = "集合")
    public R list(Map<String, Object> params) {
        return R.ok(sysParamsService.selectList(params));
    }


    @PostMapping
    @SaCheckPermission(value = "system.params.add")
    @Operation(summary = "新增")
    @Log(title = "参数管理", content = "新增")
    public R insert(@RequestBody SysParamsDTO sysParamsDTO) {
        return R.ok(sysParamsService.save(SysParamsWrapper.build().dtoEntity(sysParamsDTO)));
    }


    @PutMapping
    @SaCheckPermission(value = "system.params.update")
    @Operation(summary = "修改")
    @Log(title = "参数管理", content = "修改")
    public R update(@RequestBody SysParamsDTO sysParamsDTO) {
        return R.ok(sysParamsService.updateById(SysParamsWrapper.build().dtoEntity(sysParamsDTO)));
    }

    @GetMapping
    @SaCheckPermission(value = "system.params.info")
    @Operation(summary = "详情")
    public R view(@RequestParam("id") String id) {
        return R.ok(sysParamsService.getById(id));
    }

    @DeleteMapping
    @SaCheckPermission(value = "system.params.delete")
    @Operation(summary = "删除")
    @Log(title = "参数管理", content = "删除")
    public R delete(String ids) {
        return R.ok(sysParamsService.removeBatchByIds(Arrays.asList(StringUtils.split(ids, ","))));
    }
}
